import router from './router'
import store from './store'
import { Message, Notification } from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css'// progress bar style
import { getToken } from '@/utils/auth' // getToken from cookie

NProgress.configure({ showSpinner: false })// NProgress Configuration

const loginRoute = "/login"
// /** 路由白名单 **/
// const whiteList = new Set([loginRoute,'/404'])// no redirect whitelist
/** 需要登陆界面 **/
// const whiteList = new Set(['/goodsInfo'])// no redirect whitelist
const whiteList = new Set(['/shopCart','/personal','/addOrder','/allOrderInfo','/completed','/payment','/deliverGoods','/receiveGoods','/browseRecord'])// no redirect whitelist
/** 权限校验 **/
router.beforeEach((to, from, next) => {
  NProgress.start();
  let token = getToken();
  let hasToken = token != 'undefined' && token != undefined && token !=null && token != '';
  if (hasToken){
    /** 已经登陆 **/
    if (to.path == loginRoute) {
      /** 去登陆界面 直接跳到首页 **/
      next({ path: '/' });
    } else {
      /** 已经登陆，放行 **/
      next();
    }
  } else {
    /** 没有登陆 **/
    if (whiteList.has(to.path)){
      /** 需要登陆 **/
      next(loginRoute);
      Notification.info({title: '提示', message: '你还没有登录，请登录后再操作'});
    } else {
      /** 不需要登陆 **/
      next();
    }
  }
  NProgress.done();
});

router.afterEach(() => {
  NProgress.done() // finish progress bar
})
